<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 订单
 *
 * @icon fa fa-circle-o
 */
class Orders extends Backend
{

    /**
     * Orders模型对象
     * @var \app\admin\model\Orders
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Orders;
        $this->view->assign("statusList", $this->model->getStatusList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $filter = $this->request->get("filter", '');
            $op = $this->request->get("op", '', 'trim');
            $filter = json_decode($filter,true);
            $op = json_decode($op,true);
            $haswhere = [];
            if (isset($filter['ordergoodsname'])){
                $haswhere['name'] = ['like','%'.$filter['ordergoodsname'].'%'];
                unset($filter['ordergoodsname']);
                unset($op['ordergoodsname']);
            }
            if (isset($filter['ordergoodsskuname'])){
                $haswhere['skuname'] = ['like','%'.$filter['ordergoodsskuname'].'%'];
                unset($filter['ordergoodsskuname']);
                unset($op['ordergoodsskuname']);
            }
            if ($haswhere){
                $order_ids = db('ordersgood')
                    ->where($haswhere)
                    ->group('order_id')
                    ->column('order_id');
                $filter['id'] = $order_ids;
                $op['id'] = 'IN';
                $this->request->get(['filter'=>json_encode($filter)]);
                $this->request->get(['op'=>json_encode($op)]);
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['user'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                $row->address = json_decode($row->address,true);
                $row->getRelation('user')->visible(['nickname']);
                $row->ordergoods = db('ordersgood')
                    ->where('order_id',$row->id)
                    ->column('name,skuname,num,countprice');
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }
    //发货
    public function send($ids=null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
            $this->error(__('You have no permission'));
        }
        $row->status = 3;
        $row->sendtime = time();
        $row->save();
        $this->success();
    }
    //已取走
    public function geted($ids=null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
            $this->error(__('You have no permission'));
        }
        $row->status = 4;
        $row->entruetime = time();
        $row->save();
        $this->success();
    }

}
